//
//  MyScrollerView.m
//  TEXT
//
//  Created by dllo on 9/25/15.
//  Copyright (c) 2015 King. All rights reserved.
//

#import "MyScrollerView.h"
#import <UIImageView+WebCache.h>
#import "Blocks.h"
@interface MyScrollerView()

@end

@implementation MyScrollerView

static CGFloat const chageImageTime = 3.0;

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        flow.minimumInteritemSpacing = 0;
        flow.minimumLineSpacing = 0;
        flow.itemSize = CGSizeMake(Screen_Width, Screen_Height * 1 / 3);
        flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.scrollView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height * 1 / 3) collectionViewLayout:flow];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.contentOffset = CGPointMake(Screen_Width, 0);
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.dataSource = self;
        [self addSubview:_scrollView];
        [_scrollView registerClass:[ScrollerCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        
        self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(Screen_Width / 3, Screen_Height * 1 / 3 - 20, 20, 20)];
        _page.currentPageIndicatorTintColor = [UIColor grayColor];
        _page.pageIndicatorTintColor = [UIColor whiteColor];
        _page.currentPage = 0;
        [self addSubview:_page];
        [self moveTimeUp];

    }
    return self;
}

- (void)moveTimeUp
{
    _moveTime = [NSTimer scheduledTimerWithTimeInterval:chageImageTime target:self selector:@selector(animalMoveImage) userInfo:nil repeats:YES];
}

- (void)stopTimeUp
{
    [_moveTime invalidate];
}

#pragma mark - collectionView 代理方法的实现 给对应位置图片
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    _page.numberOfPages = _imageArr.count - 2;
    return _imageArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ScrollerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[self.imageArr objectAtIndex:indexPath.row]]];
    cell.title.text = [self.titleArr objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - 计时器到时,系统滚动图片
- (void)animalMoveImage
{
    [self.scrollView setContentOffset:CGPointMake(Screen_Width * (_page.currentPage + 2), 0) animated:YES];
    CATransition *tran = [CATransition animation];
    tran.duration = chageImageTime;
//    tran.type = @"cube";
    tran.repeatCount = NSIntegerMax;
    tran.subtype = kCATransitionFromRight;
//    [_scrollView.layer addAnimation:tran forKey:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger i = Screen_Width / 2;
    self.page.currentPage = (scrollView.contentOffset.x - Screen_Width - i) / Screen_Width + 1;
    if (self.scrollView.contentOffset.x <= 0)
    {
        self.scrollView.contentOffset = CGPointMake(Screen_Width * (self.imageArr.count - 2), 0);
    }
    else if(self.scrollView.contentOffset.x >= Screen_Width * (self.imageArr.count - 1))
    {
        self.scrollView.contentOffset = CGPointMake(Screen_Width, 0);
    }

}

#pragma mark - 停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self moveTimeUp];
}

#pragma mark - 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimeUp];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *dic = [self.block.contents objectAtIndex:indexPath.item];
    
    [self.delegate passToTableViewWithIdNum:[dic objectForKey:@"mid"]];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
